package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.QualityDefectGroup;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.QualityDefectGroupDao;
import com.efast.dmom.mes.datahandlers.base.QualityBaseDataHandler;
import com.efast.dmom.mes.entity.QualityDefectGroupEntity;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
@Component
public class QualityDefectGroupHandler extends QualityBaseDataHandler<QualityDefectGroup>{

	
	@Autowired
	private QualityDefectGroupDao qualityDefectGroupDao;
	
	
	@Override
	public ResultBean save(QualityDefectGroup obj) {
		ResultBean resultBean = new ResultBean();
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		qualityDefectGroupDao.save(obj.getInnerData());
		return resultBean;
	}
	
	@Override
	public ResultBean update(QualityDefectGroup obj) {
		ResultBean resultBean = new ResultBean();
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		qualityDefectGroupDao.update(obj.getInnerData());
		return resultBean;
	}

	public void batchDeleteByIds(List<QualityDefectGroup> objs) {
		List<QualityDefectGroupEntity> list = new ArrayList<QualityDefectGroupEntity>();
		for (QualityDefectGroup obj : objs) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowTrxId(1);
			list.add(obj.getInnerData());
		}		
		qualityDefectGroupDao.deleteBatch(list);
	}

	public QualityDefectGroup getById(long id) {
		QualityDefectGroup  qualityDefectGroup = new  QualityDefectGroup();
		qualityDefectGroup.setInnerData(qualityDefectGroupDao.getById(id));
		return qualityDefectGroup;
	}

	public QualityDefectGroup getByName(String name) {
		QualityDefectGroup  qualityDefectGroup = new  QualityDefectGroup();
		qualityDefectGroup.setInnerData(qualityDefectGroupDao.getByName(name));
		return qualityDefectGroup;
	}

	public QualityDefectGroup getByCode(String partGroupCode) {
		 QualityDefectGroup  qualityDefectGroup = new  QualityDefectGroup();
		 qualityDefectGroup.setInnerData(qualityDefectGroupDao.getByCode(partGroupCode));
		return qualityDefectGroup;
	}

	// 通过缺陷id到缺陷分组表中查询数据
		public List<QualityDefectGroup> getByDefectId(List<Long> list){
			List<QualityDefectGroup> defectBeanList = new ArrayList<QualityDefectGroup>();
			List<Long> entityList = new ArrayList<Long>();
			list.forEach(item -> {
				entityList.add(item);
			});
			List<QualityDefectGroupEntity> byDefectId = qualityDefectGroupDao.getByDefectId(entityList);
			if (byDefectId.size()>0) {
				byDefectId.forEach(item -> {
					QualityDefectGroup qualityDefectGroup = new QualityDefectGroup();
					qualityDefectGroup.setInnerData(item);
					defectBeanList.add(qualityDefectGroup);
				});
			}
			return defectBeanList;
		}
		
		// 通过缺陷id到缺陷分组表中查询数据
				public List<QualityDefectGroup> getByDefectIdAndGroupId(List<QualityDefectGroup> list){
					List<QualityDefectGroup> defectBeanList = new ArrayList<QualityDefectGroup>();
					List<QualityDefectGroupEntity> entityList = new ArrayList<QualityDefectGroupEntity>();
					list.forEach(item -> {
						entityList.add(item.getInnerData());
					});
					List<QualityDefectGroupEntity> byDefectId = qualityDefectGroupDao.getByDefectIdAndGroupId(entityList);
					if (byDefectId.size()>0) {
						byDefectId.forEach(item -> {
							QualityDefectGroup qualityDefectGroup = new QualityDefectGroup();
							qualityDefectGroup.setInnerData(item);
							defectBeanList.add(qualityDefectGroup);
						});
					}
					return defectBeanList;
				}
		
		
		// 通过分组id到缺陷分组表中查询数据
		public List<QualityDefectGroup> getByGroupId(List<Long> list){
		List<QualityDefectGroup> defectBeanList = new ArrayList<QualityDefectGroup>();
		List<QualityDefectGroupEntity> byGroupId = qualityDefectGroupDao.getByGroupId(list);
		if (byGroupId.size()>0) {
			byGroupId.forEach(item -> {
				QualityDefectGroup qualityDefectGroup = new QualityDefectGroup();
				qualityDefectGroup.setInnerData(item);
				defectBeanList.add(qualityDefectGroup);
			});
		}
		return defectBeanList;

		}

		@Override
		public void batchDeleteById(List<QualityDefectGroup> list) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public ResultBean delete(QualityDefectGroup obj) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ResultBean saveBatch(List<QualityDefectGroup> obj) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ResultBean updateBatch(List<QualityDefectGroup> obj) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ResultBean deleteBatch(List<QualityDefectGroup> obj) {
			// TODO Auto-generated method stub
			return null;
		}
}
